<?php
class Default_Model_Ajax extends Zend_Db_Table{
	
	protected $_name = 'users';
	protected $_primary ='id';
	protected $_ids;
	
	public function saveItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'permission-add'){
			$table 	= 'privileges';
			$bind	= array(
						'name' 		 => stripslashes($arrParam['name']),
						'module'	 => stripslashes($arrParam['p_module']),
						'controller' => stripslashes($arrParam['p_controller']),
						'action'	 => stripslashes($arrParam['p_action'])
					);
			$db->insert($table, $bind);
		}
	}
	
	public function countItemLichhoc($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
	
		if($options['task'] == 'admin-list'){
			$select = $db	-> select()
			-> from('nam_daotao_lichhoc AS lh', array('COUNT(lh.id) AS totalItem'));
	
			if(!empty($arrParam['id_diadiemhoc'])){
				$select->where('lh.id_diadiemhoc = ?', $arrParam['id_diadiemhoc']);
			}
	
			if(!empty($arrParam['id_phonghoc'])){
				$select->where('lh.id_phonghoc = ?', $arrParam['id_phonghoc']);
			}
			
			if(!empty($arrParam['thoigian'])){
				if($arrParam['thoigian'] == 1){
					$select->where('lh.gio_tu < ?', 12);
				}
				if($arrParam['thoigian'] == 2){
					$select->where('lh.gio_tu >= ?', 12);
					$select->where('lh.gio_tu < ?', 18);
				}
				if($arrParam['thoigian'] == 3){
					$select->where('lh.gio_tu >= ?', 18);
				}
			}
			
			if(!empty($arrParam['thu'])){
				$optionThu				= array('2' => 'Thứ 2', '3' => 'Thứ 3', '4' => 'Thứ 4', '5' => 'Thứ 5', '6' => 'Thứ 6', '7' => 'Thứ 7', '8' => 'Chủ nhật');
				$select->where("lh.thu = '" . $optionThu[$arrParam['thu']] . "'");
			}
			
			if(!empty($arrParam['id_lichhoc'])){
				$select->where('lh.id NOT IN(' . $arrParam['id_lichhoc'] . ')');
			}
	
			$result = $db	-> fetchOne($select);
		}
	
		return $result;
	}
	
	public function listItemLichhoc($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
	
		if($options['task'] == 'admin-list'){
	
			$paginator = $arrParam['paginator'];
	
			$select = $db	->	select()
							->	from('nam_daotao_lichhoc AS lh')
							->	joinLeft('nam_daotao_diadiemhoc AS ddh', 'lh.id_diadiemhoc = ddh.id',array('CONCAT(ddh.name," (",diachi,")") AS diadiemhoc_name'))
							->	joinLeft('nam_daotao_phonghoc AS ph', 'lh.id_phonghoc = ph.id',array('ph.name AS phonghoc_name'))
							->	order('lh.id_diadiemhoc ASC');
	
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
	
			if(!empty($arrParam['id_diadiemhoc'])){
				$select->where('lh.id_diadiemhoc = ?', $arrParam['id_diadiemhoc']);
			}
	
			if(!empty($arrParam['id_phonghoc'])){
				$select->where('lh.id_phonghoc = ?', $arrParam['id_phonghoc']);
			}
			
			if(!empty($arrParam['thoigian'])){
				if($arrParam['thoigian'] == 1){
					$select->where('lh.gio_tu < ?', 12);
				}
				if($arrParam['thoigian'] == 2){
					$select->where('lh.gio_tu >= ?', 12);
					$select->where('lh.gio_tu < ?', 18);
				}
				if($arrParam['thoigian'] == 3){
					$select->where('lh.gio_tu >= ?', 18);
				}
			}
			
			if(!empty($arrParam['thu'])){
				$optionThu				= array('2' => 'Thứ 2', '3' => 'Thứ 3', '4' => 'Thứ 4', '5' => 'Thứ 5', '6' => 'Thứ 6', '7' => 'Thứ 7', '8' => 'Chủ nhật');
				$select->where("lh.thu = '" . $optionThu[$arrParam['thu']] . "'");
			}
			
			if(!empty($arrParam['id_lichhoc'])){
				$select->where('lh.id NOT IN(' . $arrParam['id_lichhoc'] . ')');
			}
	
			$result = $db	-> fetchAll($select);
		}
		
		if($options['task'] == 'admin-list-chon'){
			
			if(!empty($arrParam['id_lichhoc'])){
				$select = $db	->	select()
								->	from('nam_daotao_lichhoc AS lh')
								->	joinLeft('nam_daotao_diadiemhoc AS ddh', 'lh.id_diadiemhoc = ddh.id',array('CONCAT(ddh.name," (",diachi,")") AS diadiemhoc_name'))
								->	joinLeft('nam_daotao_phonghoc AS ph', 'lh.id_phonghoc = ph.id',array('ph.name AS phonghoc_name'))
								->	where('lh.id IN(' . $arrParam['id_lichhoc'] . ')')
								->	order('lh.id_diadiemhoc ASC');
			
				$result = $db	-> fetchAll($select);
			}else{
				$result = array();
			}
		}
	
		return $result;
	
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
	
		if($options['task'] == 'admin-khachhang'){
	
			$select = $db	->	select()
							->	from('khachhang AS kh', array('*'))
							->	joinLeft('khachhang_nhom AS khn', 'khn.id = kh.id_cat', array('khn.name AS category_name'))
							->	joinLeft('nam_hopdong AS hd', 'hd.id_kh = kh.id', array('COUNT(DISTINCT(hd.id)) AS soluonghopdong'))
							->	joinLeft('nam_daotao_khoahoc AS dtkh', 'dtkh.id = hd.id_khoahoc', array('dtkh.id AS id_khoahoc', 'dtkh.name AS khoahoc_name'))
							->	group('kh.id')
							->	order('kh.id DESC')
							->	limit(10000,0);
			
			if($arrParam['id_khoahoc_clb'] == 1){
				$select->orWhere('kh.id_clb != ?', 0);
			}
			if($arrParam['id_khtn'] == 1){
				$select->orWhere('kh.status_tiemnang = ?', 1);
			}
			if($arrParam['id_khktd'] == 1){
				$select->orWhere('kh.status_tiemnang = ?', 0);
			}
			if(!empty($arrParam['id_khoahoc'])){
				$select->orWhere('dtkh.id IN (' . $arrParam['id_khoahoc'] . ')');
			}

			$result = $db	-> fetchAll($select);
		}
		return $result;
	
	}
}